package com.vitaf.cash.app.base.dialog

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

/**
 * 支持 DataBinding 的 DialogFragment 基类
 * @param T ViewDataBinding 类型
 * @param V ViewModel 类型
 */
abstract class BaseBindingDialogFragment<T : ViewDataBinding, V : ViewModel> : BaseDialogFragment<T>() {

    protected lateinit var viewModel: V

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initViewModel()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view,savedInstanceState)
        initDataBinding()
    }

    /**
     * 初始化 ViewModel
     */
    protected open fun initViewModel() {
        viewModel = ViewModelProvider(this)[getViewModelClass()]
    }

    /**
     * 初始化 DataBinding
     */
    protected open fun initDataBinding() {
        binding.setVariable(getBindingVariable(), viewModel)
        binding.lifecycleOwner = this
    }

    /**
     * 获取 ViewModel 类
     */
    protected abstract fun getViewModelClass(): Class<V>

    /**
     * 获取 Binding Variable
     */
    protected abstract fun getBindingVariable(): Int
}